package com.zhxs.utils;

import android.util.Log;


public class LogUtil {
    private static boolean isDebug = true;
    public static final int DEBUG_LEVEL_OFF = 0;
    public static final int DEBUG_LEVEL_ERROR = 1;
    public static final int DEBUG_LEVEL_WARNING = 2;
    public static final int DEBUG_LEVEL_INFO = 3;
    public static final int DEBUG_LEVEL_DEBUG = 4;
    public static final int DEBUG_LEVEL_VERBOSE = 5;
    private static String TAG = LogUtil.class.getSimpleName();
    private static int LOG_LEVEL = 9;

    public LogUtil() {
    }

    public void tag(String tag) {
        if (tag != null) {
            TAG = tag;
        } else {
            TAG = LogUtil.class.getSimpleName();
        }

    }

    public static void logLevel(int level) {
        LOG_LEVEL = level;
    }

    public static int logLevel() {
        return LOG_LEVEL;
    }

    public static void v() {
        if (!isDebug)
            return;
        if (LOG_LEVEL >= 5) {
            Log.v(TAG, getMetaInfo());
        }

    }

    public static void v(String message) {
        if (!isDebug)
            return;
        if (LOG_LEVEL >= 5) {
            Log.v(TAG, getMetaInfo() + null2str(message));
        }

    }

    public static void v(String TAG, String message) {
        if (!isDebug)
            return;
        if (LOG_LEVEL >= 5) {
            Log.v(TAG, getMetaInfo() + null2str(message));
        }
    }

    public static void d() {
        if (!isDebug)
            return;
        if (LOG_LEVEL >= 4) {
            Log.d(TAG, getMetaInfo());
        }
    }

    public static void d(String message) {
        if (!isDebug)
            return;
        if (LOG_LEVEL >= 4) {
            Log.d(TAG, getMetaInfo() + null2str(message));
        }
    }

    public static void d(String TAG, String message) {
        if (!isDebug)
            return;
        if (LOG_LEVEL >= 4) {
            Log.v(TAG, getMetaInfo() + null2str(message));
        }
    }

    public static void i() {
        if (!isDebug)
            return;
        if (LOG_LEVEL >= 3) {
            Log.i(TAG, getMetaInfo());
        }
    }

    public static void i(String message) {
        if (!isDebug)
            return;
        if (LOG_LEVEL >= 3) {
            Log.i(TAG, getMetaInfo() + null2str(message));
        }
    }

    public static void i(String TAG, String message) {
        if (!isDebug)
            return;
        if (LOG_LEVEL >= 3) {
            Log.v(TAG, getMetaInfo() + null2str(message));
        }
    }

    public static void w(String message) {
        if (!isDebug)
            return;
        if (LOG_LEVEL >= 2) {
            Log.w(TAG, getMetaInfo() + null2str(message));
        }
    }

    public static void w(String message, Throwable e) {
        if (!isDebug)
            return;
        if (LOG_LEVEL >= 2) {
            Log.v(TAG, getMetaInfo() + null2str(message));
        }
    }

    public static void w(String TAG, String message) {
        if (!isDebug)
            return;
        if (LOG_LEVEL >= 2) {
            Log.v(TAG, getMetaInfo() + null2str(message));
        }
    }

    public static void e(String message) {
        if (!isDebug)
            return;
        if (LOG_LEVEL >= 1) {
            Log.e(TAG, getMetaInfo() + null2str(message));
        }

    }

    public static void e(String message, Throwable e) {
        if (!isDebug)
            return;
        if (LOG_LEVEL >= 1) {
            Log.e(TAG, getMetaInfo() + null2str(message), e);
            printThrowable(e);
            if (e.getCause() != null) {
                printThrowable(e.getCause());
            }
        }
    }

    public static void e(String TAG, String message, Throwable e) {
        if (!isDebug)
            return;
        if (LOG_LEVEL >= 1) {
            Log.e(TAG, getMetaInfo() + null2str(message), e);
            printThrowable(e);
            if (e.getCause() != null) {
                printThrowable(e.getCause());
            }
        }
    }

    public static void e(Throwable e) {
        if (!isDebug)
            return;
        if (LOG_LEVEL >= 1) {
            printThrowable(e);
            if (e.getCause() != null) {
                printThrowable(e.getCause());
            }
        }
    }

    private static String null2str(String string) {
        return string == null ? "(null)" : string;
    }

    private static void printThrowable(Throwable e) {
        Log.e(TAG, e.getClass().getName() + ": " + e.getMessage());
        StackTraceElement[] var1 = e.getStackTrace();
        int var2 = var1.length;

        for(int var3 = 0; var3 < var2; ++var3) {
            StackTraceElement element = var1[var3];
            Log.e(TAG, "  at " + getMetaInfo(element));
        }

    }

    private static String getMetaInfo() {
        StackTraceElement element = Thread.currentThread().getStackTrace()[4];
        return getMetaInfo(element);
    }

    public static String getMetaInfo(StackTraceElement element) {
        String fullClassName = element.getClassName();
        String simpleClassName = fullClassName.substring(fullClassName.lastIndexOf(".") + 1);
        String methodName = element.getMethodName();
        int lineNumber = element.getLineNumber();
        String metaInfo = "[" + simpleClassName + "#" + methodName + ":" + lineNumber + "]";
        return metaInfo;
    }
}
